#include"Stack.h"

bool isValid(char* s) {
    Stack a;
    StackInit(&a);
    while (*s>0)
    {
        if (*s == '(' || *s == '[' || *s == '{')
        {
            StackPush(&a, *s);
        }
        else
        {
            if (StackEmpty(&a)>0)
            {
                StackDestroy(&a);
                return false;
            }

            STDataType top = StackTop(&a);
            StackPop(&a);

            if ((top == '(' && *s != ')')
                || (top == '{' && *s != '}')
                || (top == '[' && *s != ']'))
            {
                StackDestroy(&a);
                return false;
            }
        }
        ++s;
    }

    bool ret = StackEmpty(&a) > 0 ? true : false;
    StackDestroy(&a);
    return ret;
}

typedef struct {
    Stack q1;
    Stack q2;
} MyQueue;

MyQueue* myQueueCreate() {
    MyQueue* q = (MyQueue*)malloc(sizeof(MyQueue));
    StackInit(&q->q1);
    StackInit(&q->q2);
    return q;
}

void myQueuePush(MyQueue* obj, int x) {
    if (!StackEmpty(&obj->q1))
    {
        StackPush(&obj->q1, x);
    }
    else {
        StackPush(&obj->q2, x);
    }
}

int myQueuePop(MyQueue* obj) {
    Stack* empty = &obj->q1;
    Stack* Notempty = &obj->q2;
    if (!StackEmpty(&obj->q1))
    {
        empty = &obj->q2;
        Notempty = &obj->q1;
    }
    while (!StackEmpty(Notempty))
    {
        StackPush(empty, StackTop(Notempty));
        StackPop(Notempty);
    }
    int tmp = StackTop(empty);
    StackPop(empty);
    return tmp;
}

int myQueuePeek(MyQueue* obj) {
    if (!StackEmpty(&obj->q1))
    {
        return StackTop(&obj->q1);
    }
    else
        return StackTop(&obj->q2);
}

bool myQueueEmpty(MyQueue* obj) {
    return StackEmpty(&obj->q1) && StackEmpty(&obj->q2);
}

void myQueueFree(MyQueue* obj) {
    StackDestroy(&obj->q1);
    StackDestroy(&obj->q2);
    free(obj);
}
int main()
{
	//Stack t;
	//StackInit(&t);
	//StackPush(&t, 1);
	//StackPush(&t, 2);
	//StackPush(&t, 3);
	//StackPush(&t, 4);
	//while (StackEmpty(&t)==0)
	//{
	//	printf("%d ", StackTop(&t));
	//	StackPop(&t);
	//}
	//StackDestroy(&t);
 /*   char s[2] = { '(',')' };
    bool ret = isValid(&s);*/
	return 0;
}